"""
#
# Flow stability for dynamic community detection https://arxiv.org/abs/2101.06131v2
#
# Copyright (C) 2021 Alexandre Bovet <alexandre.bovet@maths.ox.ac.uk>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


create the temporal network (ContTempNetwork) for the primary school dataset
from the sociopattern data

network saved as `primaryschoolnet.pickle`
    

"""

import sys
import os
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

import pandas as pd
import numpy as np

from TemporalNetwork import ContTempNetwork
from datetime import timedelta

raise Exception
#%% read the raw data from sociopattern


df = pd.read_csv('../paper_data/socio_pat_primary_school/primaryschool.csv',
                 header=None, sep='\t', names=['time','id1','id2','class1','class2'])

df_meta = pd.read_csv('../paper_data/socio_pat_primary_school/metadata_primaryschool.txt', 
                      header=None, sep='\t', names=['id','class','sex'])


#%% network

df.sort_values(['time'], inplace=True)



net = ContTempNetwork(source_nodes=df.id1.tolist(),
                      target_nodes=df.id2.tolist(),
                      starting_times=df.time.tolist(),
                      ending_times=(df.time + 20).tolist())

net._compute_time_grid()

net.start_date = pd.datetime(2009,10,1,8,45)

times = [net.start_date + timedelta(seconds=float(net.times[i] - net.times[0])) for i in range(len(net.times))]
net.datetimes=pd.Series(data=times)

female_ids = df_meta.loc[df_meta.sex == 'F'].id.tolist()
male_ids = df_meta.loc[df_meta.sex == 'M'].id.tolist()


net.node_label_array = np.array([net.node_to_label_dict[n] for n in net.node_array])
net.male_array = np.isin(net.node_label_array, male_ids)
net.female_array = np.isin(net.node_label_array, female_ids)
net.node_class_array = np.array([df_meta.loc[df_meta.id == label,'class'].values[0] \
                                 for label in net.node_label_array])

net.node_first_start_array = np.array([df.loc[(df.id1 == label) | (df.id2 == label)].time.min() \
                                               for label in net.node_label_array])

net.node_last_end_array = np.array([df.loc[(df.id1 == label) | (df.id2 == label)].time.max()+20 \
                                               for label in net.node_label_array])

net._merge_overlapping_events(verbose=True)
net._overlapping_events_merged = True
net._compute_time_grid()
#%%
filename = '../paper_data/socio_pat_primary_school/primaryschoolnet.pickle'
net.save(filename=filename,
         attributes_list = ['node_to_label_dict',
                      'events_table',
                      'times',
                      'time_grid',
                      'num_nodes',
                      '_overlapping_events_merged',
                      'start_date',
                      'node_label_array',
                      'male_array',
                      'female_array',
                      'node_first_start_array',
                      'node_last_end_array',
                      'node_class_array',
                      'datetimes'])

